<?php

//防止恶意调用
if (!defined('IN_MOD'))
{
    die('Hacking attempt');
}

// 检查登录
$this->checkUserLogin();

$type = moduleaqvarval("type","string");

// 获取商家已发布的分类
function getsupplierpubcate()
{
	global $configinfo;
	
	// 已发布的分类
	$modulewhere=array();
	$modulewhere['tablename'] = "fastxcms_portal_post";
	$modulewhere['limit'] = "all";
	$modulewhere['order'] = "default";
	$modulewhere['supplierok'] = 1;
	$supplierdatalist = configmodule($modulewhere);	
	$supplierpubcate = array();
	foreach($supplierdatalist as $key=>$val)
	{
		$supplierpubcate[$val['category_id']]=1;
	}
	
	// 平台分类
	$supplierpubcatedata = array();
	foreach($configinfo['modulecolumn']['list'] as $key=>$val)
	{
		foreach($val['field2'] as $key2=>$val2)
		{
			if(empty($supplierpubcate[$val2['id']]))
			{
				unset($val['field2'][$key2]);	
			}
		}
		if(count($val['field2']) > 0)
		{
			$supplierpubcatedata[$key] = $val;
		}
	}
	
	return $supplierpubcatedata;
}

// 信息列表
if($type == 'list')
{
	$configinfo['smartyname'] = "list/supplier_list";// 设置切换模板

	$supplierpubcate = getsupplierpubcate();
	$this->assign('supplierpubcate',    $supplierpubcate);
}

// 信息分类
if($type == 'cate')
{
	$configinfo['smartyname'] = "list/supplier_cate";// 设置切换模板
}

// 信息添加
if($type == 'add')
{
	$modulehook = "citytab";$hookblock = "pubinfo";require(PLUGINS_PATH . 'modulepara/config/modulehook.php');// 配置系统钩子
	
	$configinfo['smartyname'] = "list/supplier_add";// 设置切换模板
	
	$modulehook = "citytab";$hookblock = "areaclass";require(PLUGINS_PATH . 'modulepara/config/modulehook.php');// 配置系统钩子
	
	// 模块参数
	$contenthtml = configmoduleparameter();
	$this->assign('contenthtml',    $contenthtml);
}

// 信息插入
if ($type == 'addpost')
{
	// 接收数据	
	$moduleaqvar = array();
	$moduleaqvar[0] = 'senddata-module';
	$moduleaqvar[1] = 'senddata-class2';
	$moduleaqvar[2] = 'senddata-class3';
	$moduleaqvar[3] = 'senddata-create_time';
	$moduleaqvar[4] = 'senddata-published_time';
	$moduleaqvar[5] = 'senddata-category_id';
	$moduleaqvar[6] = 'senddata-cityid';
	$moduleaqvar[7] = 'senddata-lang';
	$moduleaqvar[8] = 'senddata-user_id';
	$moduleaqvar[9] = 'senddata-user_name';
	$moduleaqcontent = array();
	$moduleaqcontent[0] = $configinfo['moduleid'];
	$moduleaqcontent[1] = $configinfo['moduleclass']['class2']['id'];
	$moduleaqcontent[2] = $configinfo['moduleclass']['class3']['id'];
	$moduleaqcontent[3] = time();
	$moduleaqcontent[4] = time();
	$moduleaqcontent[5] = $configinfo['columnid'];
	$moduleaqcontent[6] = $configbase['cityid'];
	$moduleaqcontent[7] = $lang;
	$moduleaqcontent[8] = $configmember['id'];
	$moduleaqcontent[9] = $configmember['user_login'];
	moduleaqvardata($moduleaqvar,$moduleaqcontent);
	
	$senddata = moduleaqvarval('senddata','array');
	
	$more = array();
	
	if (!empty($_MGPC_REQ['photo_names']) && !empty($_MGPC_REQ['photo_urls'])) {
		$more['photos'] = [];
		foreach ($_MGPC_REQ['photo_urls'] as $key => $url) {
			$photoUrl = cmf_asset_relative_url($url);
			array_push($more['photos'], ["url" => $photoUrl, "name" => $_MGPC_REQ['photo_names'][$key]]);
		}
	}
	
	if (!empty($_MGPC_REQ['file_names']) && !empty($_MGPC_REQ['file_urls'])) {
		$more['files'] = [];
		foreach ($_MGPC_REQ['file_urls'] as $key => $url) {
			$fileUrl = cmf_asset_relative_url($url);
			array_push($more['files'], ["url" => $fileUrl, "name" => $_MGPC_REQ['file_names'][$key]]);
		}
	}
	
	$senddata['more'] = json_encode($more);
	
	// 插入数据
	$insert_id = insertdata('fastxcms_portal_post', $senddata);
	updatedata('fastxcms_portal_post',"`update_time` = '".time()."'",$configinfo['article_id'],$insert_id);
	insertmoduleparameter($insert_id);
	
	$this->success($configlang['Submit_data_inserted_successfully'],cmf_url('portal/list/index',array('id'=>$configinfo['moduleid']))."?act=supplier&type=list");
}

// 信息编辑
if($type == 'edit')
{
	$configinfo['smartyname'] = "list/supplier_edit";// 设置切换模板
	$actid = moduleaqvarval("actid", "intval");
	
	// 当前信息
	$modulewhere=array();
	$modulewhere[0]=$configinfo['ecs_article'];
	$modulewhere[1]=$actid;
	$modulewhere[2]="id";
	$data=modulepara("datainfo",$modulewhere);	
	
	$modulehook = "citytab";$hookblock = "areaclass";require(PLUGINS_PATH . 'modulepara/config/modulehook.php');// 配置系统钩子
	
	// 模块参数
	$contenthtml = configmoduleparameter($actid);
	$this->assign('contenthtml',    $contenthtml);
	global $parameter_default;
	$this->assign('data',    $parameter_default);
}

// 信息插入
if ($type == 'editpost')
{
	$actid = moduleaqvarval("actid", "intval");
	
	$senddata = moduleaqvarval('senddata','array');
	
	$more = array();
	
	if (!empty($_MGPC_REQ['photo_names']) && !empty($_MGPC_REQ['photo_urls'])) {
		$more['photos'] = [];
		foreach ($_MGPC_REQ['photo_urls'] as $key => $url) {
			$photoUrl = cmf_asset_relative_url($url);
			array_push($more['photos'], ["url" => $photoUrl, "name" => $_MGPC_REQ['photo_names'][$key]]);
		}
	}
	
	if (!empty($_MGPC_REQ['file_names']) && !empty($_MGPC_REQ['file_urls'])) {
		$more['files'] = [];
		foreach ($_MGPC_REQ['file_urls'] as $key => $url) {
			$fileUrl = cmf_asset_relative_url($url);
			array_push($more['files'], ["url" => $fileUrl, "name" => $_MGPC_REQ['file_names'][$key]]);
		}
	}
	
	$senddata['more'] = json_encode($more);

	// 修改数据
	updatedata('fastxcms_portal_post',$senddata,$configinfo['article_id'],$actid);
	updatedata('fastxcms_portal_post',"`update_time` = '".time()."'",$configinfo['article_id'],$actid);
	updatemoduleparameter($actid);
	
	$this->success("帖子修改成功！",cmf_url('portal/list/index',array('id'=>$configinfo['moduleid']))."?act=supplier&type=list");
}

// 信息删除
if ($type == 'del')
{	
	$actid = moduleaqvarval("actid", "intval");
	
	// 删除数据
	deletedata("fastxcms_portal_post", "id", $actid);

	// 删除相关参数
	deletemoduleparameter($actid);
	
	$this->success("帖子删除成功！",cmf_url('portal/list/index',array('id'=>$configinfo['moduleid']))."?act=supplier&type=list");
}

?>